package com.codingke.codingbookstore.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.codingke.codingbookstore.R;
import com.codingke.codingbookstore.vo.Address;
import com.codingke.codingbookstore.vo.User;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

public class AddAddressActivity extends BaseActivity {
    private EditText editText_username,editText_phoneNumber,editText_address;
    private Address address;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_address);
        editText_username = (EditText) findViewById(R.id.editText_username);
        editText_phoneNumber = (EditText) findViewById(R.id.editText_phoneNumber);
        editText_address = (EditText) findViewById(R.id.editText_address);
    }
    
    public void saveAddressClick(View view){
        saveAddress();
    }
    public void saveAndDefaultAddressClick(View view){
        saveAndSetDefault();
    }

    /**
     * 保存地址
     */
    private void saveAddress() {
        createAddress(false);
    }

    private void createAddress(boolean isDefault) {
        String username = editText_username.getText().toString();
        if (TextUtils.isEmpty(username)){
            showToast("请输入收货人姓名");
            return;
        }
        String phoneNumber = editText_phoneNumber.getText().toString();
        if (TextUtils.isEmpty(phoneNumber)){
            showToast("请输入收货人手机号码");
            return;
        }
        String addressInfo = editText_address.getText().toString();
        if (TextUtils.isEmpty(addressInfo)){
            showToast("请输入收货人详细地址");
            return;
        }
        address = new Address();
        address.setUsername(username);
        address.setPhoneNumber(phoneNumber);
        address.setAddress(addressInfo);
        address.setUserId(BmobUser.getCurrentUser(this,User.class).getObjectId());
        address.setIsDefault(isDefault);
        address.save(this, new SaveListener() {
            @Override
            public void onSuccess() {
                showToast("保存地址成功");
                finish();
            }
            @Override
            public void onFailure(int i, String s) {
                showToast("保存地址失败");
            }
        });
    }

    /**
     * 保存地址及设置为默认
     */
    private void saveAndSetDefault() {
        createAddress(true);
        //修改默认地址为非默认
        BmobQuery<Address> query = new BmobQuery<>();
        query.addWhereEqualTo("userId", BmobUser.getCurrentUser(this,User.class).getObjectId());
        query.addWhereEqualTo("isDefault",true);
        query.findObjects(this, new FindListener<Address>() {
            @Override
            public void onSuccess(List<Address> list) {
                Address temp;
                for (int i=0;i<list.size();i++){
                    temp = list.get(i);
                    temp.setIsDefault(false);
                    temp.update(AddAddressActivity.this);
                }
            }
            @Override
            public void onError(int i, String s) {

            }
        });
    }
}
